package cn.itcast.bos.index;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;

public class WayBillIndexRepositoryServiceImpl implements WayBillIndexRepositoryService{
	@Autowired
	private WayBillRepository wayBillRepository;
	
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;

	@Override
	public void save(WayBill waybill) {
		//通过运单号获取运单
		WayBill persistWayBill = wayBillIndexRepository.findByWayBillNum(waybill.getWayBillNum());
		//判断运单号是否存在，此处做的判断可以防止空指针异常，即persistWayBill可能为空，直接获取其id会报空指针异常
		if (persistWayBill==null||persistWayBill.getId()==null) {
			wayBillRepository.save(waybill);
			wayBillIndexRepository.save(waybill);
		}else {
			try {
				Integer id = persistWayBill.getId();
				BeanUtils.copyProperties(persistWayBill, waybill);
				persistWayBill.setId(id);	
				wayBillIndexRepository.save(persistWayBill);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
	
	}
}
